package com.hmily.dubbo.provider.impl;

import com.hmily.dubbo.api.UserQueryFacade;
import com.hmily.dubbo.api.bean.UserInfo;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.util.StringUtils;

/**
 * <h1>用户查询实现类逻辑。</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-10-18
 */
public class UserQueryFacadeImpl implements UserQueryFacade {

    @Override
    public String queryUser(String name) {
        RpcContext context = RpcContext.getContext();
        String reqName = "attachment's key = name, value = " + context.getAttachment("name");
        String msg = "hello " + name + ", my name is yuki, my nickname is xyz, port = " + context.getLocalPort() + "["+System.currentTimeMillis()+"]";
        if(!StringUtils.isEmpty(reqName)){
            msg = reqName + ", " + msg;
        }
        System.out.println(msg);
        return msg;
    }

    @Override
    public UserInfo queryUserInfo(UserInfo userInfo) {
        int localPort = RpcContext.getContext().getLocalPort();
        System.out.println("recv, userInfo " + userInfo + ", port = " + localPort);

        // 针对入参做一些简单的赋值操作，模拟进行了业务逻辑处理
        userInfo.setId(localPort + "----" + userInfo.getId());
        userInfo.setName(localPort + "----" + userInfo.getName());
        userInfo.setSex(localPort + "----" + userInfo.getSex());

        System.out.println("resp, userInfo " + userInfo + ", port = " + localPort);
        return userInfo;
    }
}
